package com.david.flowable.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

public class DateUtils {

    public static LocalDateTime getLocalDateTime(Date date) {
        if (Objects.isNull(date)){
            return null;
        } else {
            return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        }
    }

    public static Date getDate(LocalDateTime localDateTime) {
        if (Objects.isNull(localDateTime)){
            return null;
        } else {
            return Date.from(localDateTime.atZone(ZoneId.of("GMT+08")).toInstant());
        }
    }

    public static String getDuration(Long millis) {
        if(Objects.isNull(millis)){
            return null;
        } else if (millis <= 0) {
            return "0秒";
        } else {
            long day = millis / 1000 / 60 / 60 / 24;
            long hour = millis / 1000 / 60 / 60 % 24;
            long minute = millis / 1000 / 60 % 60;
            long second = millis / 1000 % 60;
            if (day > 0) return day + "天" + hour + "小时" + minute + "分" + second + "秒";
            if (hour > 0) return hour + "小时" + minute + "分" + second + "秒";
            if (minute > 0) return minute + "分" + second + "秒";
            else return second + "秒";
        }
    }

}
